//
//  NowFishViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/8/7.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "NowFishViewController.h"
#import "MainTableViewCell.h"
#import "JuNengLolViewController.h"  // 新闻详情
#import "GoodsDetailsViewController.h"

@interface NowFishViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,HTHorizontalSelectionListDataSource,HTHorizontalSelectionListDelegate>
{
    
    UITableView * _tableView;
    float aotoHeight;
    
    
    NSMutableArray * _titileArray;
    NSMutableArray * _titleKeyArray;
    //创建的ui
    UIScrollView * _topScrollView;
    CGPoint _point; //设置偏移量时使用
    UIScrollView * _scrollView;//内容上的滑块
    
    
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
    
    NSInteger currInteger;
    
    NSArray * bannerDataArray;
    
    NSString * curryTypeStr;
}

@property(nonatomic,strong)HTHorizontalSelectionList * selectionList;

@end

@implementation NowFishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(self.titleStr)
    
    curryTypeStr = @"";
    currInteger = 0;
    _page =1;
    _dataArray = @[].mutableCopy;
    _titileArray = @[].mutableCopy;
    _titleKeyArray = @[].mutableCopy;
    
    [self creatTableView];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    [self getDataRequestFor:curryTypeStr];
    [self getBannerNetWorking];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[MainTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MainTableViewCell reuseIdentifier]];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page =1;
        [self getDataRequestFor:curryTypeStr];
        
    }];
    _tableView.mj_header =header;
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getDataRequestFor:curryTypeStr];
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    _tableView.mj_footer.hidden = NO;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    aotoHeight = (WIDTH -40)/3 * 1.25;
    
    return 210;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 120;

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    MainTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[MainTableViewCell reuseIdentifier] forIndexPath:indexPath];
    [cell.newsImage sd_setImageWithURL:ImageUrl(dic[@"img_src"]) placeholderImage:ImageName(@"")];
    cell.newsTitle.text = dic[@"title"];
    cell.timeLabel.text = dic[@"create_time"];
    cell.newsDetails.text = dic[@"brief"];
    
    cell.newsDetails.frame = CGRectMake(125, 44, WIDTH-135, 20);
    cell.newsDetails.numberOfLines = 2;
    [cell.newsDetails sizeToFit];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = _dataArray[indexPath.row];
    JuNengLolViewController * details = [[JuNengLolViewController alloc]init];
    details.newsIdStr = dic[@"news_id"];
    [self.navigationController pushViewController:details animated:YES];

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 210)];
    heardView.backgroundColor =[UIColor whiteColor];
    
    NSMutableArray * imageArrray = @[].mutableCopy;
    if (bannerDataArray.count>0) {
        
        if (imageArrray.count>0) {
            
            [imageArrray removeAllObjects];
        }
        
        for (NSDictionary * dic in bannerDataArray) {
            
            [imageArrray addObject:dic[@"src"]];
        }
    }

    
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH, 160) delegate:self placeholderImage:[UIImage imageNamed:@"test_banner_icon"]];
    cycleScrollView.backgroundColor =Color(229, 229, 229);
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView.currentPageDotColor = NavBackColor;
    //    cycleScrollView.imageURLStringsGroup = imageArrray;
    cycleScrollView.imageURLStringsGroup = imageArrray;
    [heardView addSubview:cycleScrollView];
    cycleScrollView.clickItemOperationBlock = ^(NSInteger index) {
        
        NSDictionary * dic = bannerDataArray[index];
        
        if ([dic[@"go_type"] isEqualToString:@"goods"]) { //商品
            
            if (verifTokenAndEnter) {
                
                GoodsDetailsViewController * goods =[[GoodsDetailsViewController alloc]init];
                goods.hidesBottomBarWhenPushed = YES;
                goods.goodsId = dic[@"go_id"];
                [self.navigationController pushViewController:goods animated:YES];
            }else {
                
                LogingViewController * login =[[LogingViewController alloc]init];
                login.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:login animated:YES];
            }
        }
        if ([dic[@"go_type"] isEqualToString:@"news"]) { //新闻
            
            JuNengLolViewController * lol =[[JuNengLolViewController alloc]init];
            lol.newsIdStr = dic[@"go_id"];
            lol.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:lol animated:YES];
        }

        
    };
    
    
    UIView * titleView =[[UIView alloc]initWithFrame:CGRectMake(0, 160, WIDTH, 50)];
    titleView.backgroundColor =[UIColor whiteColor];
    
    _selectionList =[[HTHorizontalSelectionList alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    _selectionList.dataSource=self;
    _selectionList.delegate=self;
    [titleView addSubview:_selectionList];
    [_selectionList setTitleFont:[UIFont systemFontOfSize:16] forState:UIControlStateSelected];
    [_selectionList setTitleFont:[UIFont systemFontOfSize:16] forState:UIControlStateNormal];
    [_selectionList setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_selectionList setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    
//    if ([typeStr integerValue]==21) {
//        
//        _selectionList.selectedButtonIndex = 0;
//    }else if ([typeStr integerValue]==22) {
//        
//        _selectionList.selectedButtonIndex = 1;
//    }else if ([typeStr integerValue]==23) {
//        
//        _selectionList.selectedButtonIndex = 2;
//    }
    if (_titileArray.count>0) {
         _selectionList.selectedButtonIndex = currInteger;
    }
   
//    
    //滑动按钮下边的滑块设置
    _selectionList.selectionIndicatorColor = [UIColor blackColor];
    _selectionList.selectionIndicatorHeight =3;
    _selectionList.bottomTrimColor =[UIColor whiteColor];
    
    [heardView addSubview:titleView];
    
    return heardView;
    
}

//pragma mark------HTHorizontalSelectionListDelegate实现--
//点击效果
-(void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index{
    
    [UIView animateWithDuration:0.6 animations:^{
        
        _scrollView.contentOffset =CGPointMake(WIDTH * index, 0);
    }];
    UITableView *tableViwe=(id)[self.view viewWithTag:1000+index];
    [tableViwe reloadData];
    currInteger = index;
    _page =1;
    [self getDataRequestFor:_titleKeyArray[index]];
    
    curryTypeStr = _titleKeyArray[index];
//    _page = 1;
//    if (index==0) {
//        typeStr = @"21";
//        self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"政府项目"];
//        [self getNetWorkingDataFor:@"21"];
//    }else if (index==1){
//        typeStr = @"22";
//        self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"企业项目"];
//        [self getNetWorkingDataFor:@"22"];
//    }else {
//        typeStr = @"23";
//        self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"个人项目"];
//        [self getNetWorkingDataFor:@"23"];
//    }
}


//返回下表对应的文字按钮
-(NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index{
    
    return _titileArray[index];
}
//多少个
-(NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList{
    
    return _titileArray.count;
    
}


-(void)getDataRequestFor:(NSString *)str {


    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"type":str,
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"page_num":@"10",
                           @"news_type":self.newsType
                           };
    
    //    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_newsList_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage = [SHJudgeTools totailpageNumberForTotailNumber:responseObject[@"datas"][@"total_count"]];
            
            if ([responseObject[@"datas"][@"types"]  isKindOfClass:[NSArray class]]) {
                
                if (_titileArray.count>0) {
                
                    [_titileArray removeAllObjects];
                    [_titleKeyArray removeAllObjects];
                }
                
                for (NSDictionary * dic in responseObject[@"datas"][@"types"]) {
                    
                    [_titileArray addObject:dic[@"dict"]];
                    [_titleKeyArray addObject:dic[@"type"]];
                }
            }
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                   
                    if (_dataArray.count<10) {
                        
                        _tableView.mj_footer.hidden = YES;
                    }else {
                        _tableView.mj_footer.hidden = NO;
                    }
                }else {
                    _tableView.mj_footer.hidden = YES;
                   
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
}


-(void)getBannerNetWorking {
    
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,get_banner_api];
    NSDictionary * dic = @{@"type":self.newsType};
    
    SHuiMBhubNoTimeLoding;
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                
                bannerDataArray = responseObject[@"datas"];
            }
            
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
        
    }];
}


@end
